/* 
 * GrayRabbit Cms基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department：运营部
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.dao.plugins.impl;

import java.util.List;

import org.grayrabbit.cms.dao.plugins.SubjectOptionDao;
import org.grayrabbit.cms.entity.SubjectOption;
import org.grayrabbit.cms.vo.plugins.SubjectOptionBean;
import org.springframework.stereotype.Repository;

import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;

/**
 * <b>SubjectOption</b> dao implementation 





 * @version 1.0，创建时间：2011-05-16 16:09:05 
 */
@Repository
public class SubjectOptionDaoImpl extends GenericHibernateDao<SubjectOption, String>
		implements SubjectOptionDao {

	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From SubjectOption as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";

	/**
	 * 根据bean组装查询条件
	 * @see SubjectOptionDao#assembly(SubjectOptionBean)
	 */
	public QueryCondition assembly(SubjectOptionBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * @see SubjectOptionDao#assemblyCount(SubjectOptionBean)
	 */
	public QueryCondition assemblyCount(SubjectOptionBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * @see SubjectOptionDao#count(SubjectOptionBean)
	 */
	public int count(SubjectOptionBean bean) {
		return ((Long)iterator(assemblyCount(bean)).next()).intValue();
	}

	/**
	 * 根据bean组装查询条件(名称参数)
	 * @see SubjectOptionDao#assemblyNameQuery(SubjectOptionBean)
	 */
	public QueryCondition assermblyNameQuery(SubjectOptionBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * @see SubjectOptionDao#assemblyNameQueryCount(SubjectOptionBean)
	 */
	public QueryCondition assemblyNameQueryCount(SubjectOptionBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * @see SubjectOptionDao#findByPage(Page, SubjectOptionBean)
	 */
	public List<SubjectOption> findByPage(Page<SubjectOption> page,
			SubjectOptionBean bean) {
		QueryCondition qc = assembly(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		return findByQc(qc);
	}
	// 业务查询主要逻辑代码
	private void transNameQuery(SubjectOptionBean bean,QueryCondition qc){
		if(bean!=null){
			             if(bean.getOptionTitle() != null){
				qc.add("And obj.optionTitle like ? ", bean.getOptionTitle()+"%");
			}
            if(bean.getOptionCount() != null){
				qc.add("And obj.optionCount like ? ", bean.getOptionCount()+"%");
			}
            if(bean.getMemo() != null){
				qc.add("And obj.memo like ? ", bean.getMemo()+"%");
			}
            if(bean.getOptionOrd() != null){
				qc.add("And obj.optionOrd like ? ", bean.getOptionOrd()+"%");
			}
            if(bean.getResultCount() != null){
				qc.add("And obj.resultCount like ? ", bean.getResultCount()+"%");
			}
		}
	}

	/**
	 *根据科目Id查询答案项
	 * @param subjectId
	 * @return
	 */
	public List<SubjectOption> findBySubjectId(String subjectId){
		QueryCondition qc = assembly(null);
		qc.add(" And obj.surveySubject.subjectId = ? ", subjectId);
		return findByQc(qc);
	}
}